package com.example.chinesechess.presentation.ui.screen

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.chinesechess.R
import java.text.SimpleDateFormat
import java.util.*

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GameRecordsScreen(navController: NavController) {
    // Mock game records data
    val gameRecords = listOf(
        GameRecord("本地对战", Date(System.currentTimeMillis() - 3600000), "红方胜"),
        GameRecord("AI对战(中等)", Date(System.currentTimeMillis() - 86400000), "红方胜"),
        GameRecord("本地对战", Date(System.currentTimeMillis() - 172800000), "黑方胜"),
        GameRecord("在线对战", Date(System.currentTimeMillis() - 259200000), "和棋"),
        GameRecord("AI对战(困难)", Date(System.currentTimeMillis() - 345600000), "黑方胜")
    )
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(stringResource(R.string.menu_game_records)) },
                navigationIcon = {
                    IconButton(onClick = { navController.popBackStack() }) {
                        // Replace with actual back icon
                        Text("<")
                    }
                }
            )
        }
    ) { padding ->
        if (gameRecords.isEmpty()) {
            // Empty state
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "暂无游戏记录",
                    style = MaterialTheme.typography.titleMedium
                )
            }
        } else {
            // List of game records
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
                    .padding(horizontal = 16.dp)
            ) {
                items(gameRecords) { record ->
                    GameRecordItem(record = record, onClick = {
                        // Navigate to game replay screen (currently just reuses game screen)
                        navController.navigate("game/replay")
                    })
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GameRecordItem(record: GameRecord, onClick: () -> Unit) {
    val dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
    
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp),
        onClick = onClick
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = record.gameType,
                    style = MaterialTheme.typography.titleMedium
                )
                
                Text(
                    text = record.result,
                    style = MaterialTheme.typography.titleSmall,
                    color = when (record.result) {
                        "红方胜" -> MaterialTheme.colorScheme.primary
                        "黑方胜" -> MaterialTheme.colorScheme.tertiary
                        else -> MaterialTheme.colorScheme.secondary
                    }
                )
            }
            
            Spacer(modifier = Modifier.height(8.dp))
            
            Text(
                text = dateFormatter.format(record.date),
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }
}

// Data class for game records
data class GameRecord(
    val gameType: String,
    val date: Date,
    val result: String
) 